package cn.tedu.em.complaint.service.impl;

import cn.tedu.em.common.exception.EstateManagementServiceException;
import cn.tedu.em.common.pojo.complaint.dto.ComplaintAddNewDTO;
import cn.tedu.em.common.pojo.complaint.dto.ComplaintUpdateDTO;
import cn.tedu.em.common.pojo.complaint.entity.Complaint;
import cn.tedu.em.common.pojo.complaint.vo.ComplaintListItemVO;
import cn.tedu.em.common.pojo.complaint.vo.ComplaintStandardVO;
import cn.tedu.em.common.restful.ResponseCode;
import cn.tedu.em.complaint.mapper.ComplaintMapper;
import cn.tedu.em.complaint.service.IComplaintService;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@DubboService
@Service
@Slf4j
public class ComplaintServiceImpl implements IComplaintService {

    @Autowired
    private ComplaintMapper complaintMapper;

    public ComplaintServiceImpl() {
        log.info("创建业务类的对象：ComplaintServiceImpl");
    }

    @Override
    public void addNew(ComplaintAddNewDTO complaintAddNewDTO) {
        log.info("开始处理【添加投诉】的业务，参数：{}", complaintAddNewDTO);

        Complaint complaint = new Complaint();
        BeanUtils.copyProperties(complaintAddNewDTO,complaint);
        int unDispose = 0;
        complaint.setDispose(unDispose);
        log.info("即将执行插入数据，参数：{}", complaint);
        int rows = complaintMapper.insert(complaint);
        if (rows != 1){
            String message = "添加投诉失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new EstateManagementServiceException(ResponseCode.INTERNAL_SERVER_ERROR, message);
        }
    }

    @Override
    public void delete(Long id) {
        log.info("开始处理【根据ID删除投诉】的业务，参数：{}", id);

        ComplaintStandardVO complaintStandardVO = complaintMapper.getStandardById(id);
        if (complaintStandardVO == null){
            String message = "删除投诉失败，尝试删除的投诉数据不存在！";
            log.warn(message);
            throw new EstateManagementServiceException(ResponseCode.NOT_FOUND,message);
        }

        log.debug("即将执行删除，参数：{}", id);
        int rows = complaintMapper.deleteById(id);
        if (rows != 1){
            String message = "删除投诉失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new EstateManagementServiceException(ResponseCode.INTERNAL_SERVER_ERROR, message);
        }

    }

    @Override
    public void SetUndispose(Long id) {
        log.debug("开始处理【未处理投诉】的业务，参数：{}", id);
        updateDisposeById(id,0);
    }

    @Override
    public void SetDispose(Long id) {
        log.debug("开始处理【已处理投诉】的业务，参数：{}", id);
        updateDisposeById(id,1);
    }

    @Override
    public void updateInfoById(Long id, ComplaintUpdateDTO complaintUpdateDTO) {
        log.debug("开始处理【修改投诉】的业务，ID：{}，新数据：{}", id, complaintUpdateDTO);
        ComplaintStandardVO complaintStandardVO= complaintMapper.getStandardById(id);

        if (complaintStandardVO==null){
            String message = "修改投诉失败，尝试修改的投诉数据不存在！";
            log.warn(message);
            throw new EstateManagementServiceException(ResponseCode.NOT_FOUND,message);
        }

        Complaint complaint = new Complaint();
        BeanUtils.copyProperties(complaintUpdateDTO,complaint);
        complaint.setId(id);
        log.debug("即将修改投诉详情：{}", complaint);
        int rows = complaintMapper.update(complaint);
        if (rows != 1){
            String message = "修改投诉失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new EstateManagementServiceException(ResponseCode.INTERNAL_SERVER_ERROR, message);
        }
    }

    @Override
    public ComplaintStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询投诉详情】的业务");
        ComplaintStandardVO complaint = complaintMapper.getStandardById(id);
        if (complaint==null){
            String message = "查询投诉详情失败，尝试访问的数据不存在！";
            log.warn(message);
            throw new EstateManagementServiceException(ResponseCode.NOT_FOUND,message);
        }
        return complaint;
    }

    @Override
    public List<ComplaintListItemVO> list() {
        log.debug("开始处理【查询投诉列表】的业务，无参数");
        List<ComplaintListItemVO> list = complaintMapper.list();
        return list;
    }

    private void updateDisposeById(Long id, Integer isDispose) {
        log.info("id:{}",id);
        ComplaintStandardVO complaint = complaintMapper.getStandardById(id);
        if (complaint == null){
            String message = "修改投诉状态失败，投诉数据不存在！";
            log.warn(message);
            throw new EstateManagementServiceException(ResponseCode.NOT_FOUND,message);
        }

        if (complaint.getDispose() == isDispose){
            String message = "修改投诉状态失败，投诉已经处于" + DISPOSE_TEXT[isDispose] + "状态！";
            log.warn(message);
            throw new EstateManagementServiceException(ResponseCode.CONFLICT,message);
        }

        Complaint updateComplaint = new Complaint();
        updateComplaint.setId(id);
        updateComplaint.setDispose(isDispose);

        int rows = complaintMapper.update(updateComplaint);
        if (rows != 1){
            String message = "修改投诉状态失败，服务器忙，请稍后再尝试！";
            log.warn(message);
            throw new EstateManagementServiceException(ResponseCode.INTERNAL_SERVER_ERROR, message);
        }
    }
}
